package com.geline.mybatisplus.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.reflect.GenericTypeUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.geline.mybatisplus.context.One2ManyFactory;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Deprecated
public class ServicesImpl<T> implements IServices<T> {
    protected final Class<?>[] typeArguments = GenericTypeUtils.resolveTypeArguments(this.getClass(), ServicesImpl.class);
    protected final Class<T> entityClass = this.currentEntityClass();

    @Resource
    protected One2ManyFactory one2ManyFactory;

    protected Class<T> currentEntityClass() {
        return (Class<T>) this.typeArguments[0];
    }

    @Override
    public <V> boolean save(V object) {
        return one2ManyFactory.save(entityClass, object);
    }

    @Override
    public <V> boolean updateById(V object) {
        return one2ManyFactory.updateById(entityClass, object);
    }

    @Override
    public boolean saveBatch(Collection<T> entityList) {
        return one2ManyFactory.saveBatch(entityList);
    }

    @Override
    public boolean removeById(Serializable id) {
        return one2ManyFactory.removeById(entityClass, id);
    }

    @Override
    public boolean removeByIds(Collection<?> ids) {
        return one2ManyFactory.removeByIds(entityClass, ids);
    }

    @Override
    public List<T> list(AbstractWrapper<T, ?, ?> queryWrapper) {
        return Db.list(queryWrapper);
    }

    @Override
    public T getById(Serializable id) {
        return one2ManyFactory.getById(entityClass, id);
    }

    @Override
    public <V> V getById(Serializable id, Class<V> voClass) {
        return one2ManyFactory.getById(entityClass, id, voClass);
    }

    @Override
    public <V> List<V> list(AbstractWrapper<T, ?, ?> queryWrapper, Class<V> voClass) {
        List<T> list = Db.list(queryWrapper);
        return BeanUtil.copyToList(list, voClass);
    }

    @Override
    public List<Map<String, Object>> listMaps(AbstractWrapper<T, ?, ?> queryWrapper) {
        return Db.listMaps(queryWrapper);
    }

    @Override
    public <V> List<V> listMaps(AbstractWrapper<T, ?, ?> queryWrapper, Class<V> voClass) {
        List<Map<String, Object>> maps = Db.listMaps(queryWrapper);
        return BeanUtil.copyToList(maps, voClass);
    }

    @Override
    public IPage<T> page(IPage page, AbstractWrapper<T, ?, ?> queryWrapper) {
        return Db.page(page, queryWrapper);
    }

    @Override
    public <V> IPage<V> page(IPage page, AbstractWrapper<T, ?, ?> queryWrapper, Class<V> voClass) {
        IPage<T> pages = Db.page(page, queryWrapper);
        List<T> beans = pages.getRecords();
        List<V> voList = BeanUtil.copyToList(beans, voClass);
        Page pageNew = new Page();
        pageNew.setCurrent(pages.getCurrent());
        pageNew.setSize(pages.getSize());
        pageNew.setTotal(pages.getTotal());
        pageNew.setPages(pages.getPages());
        pageNew.setRecords(voList);
        return pageNew;
    }

    @Override
    public IPage<Map<String, Object>> pageMaps(IPage page, AbstractWrapper<T, ?, ?> queryWrapper) {
        return Db.pageMaps(page, queryWrapper);
    }

    @Override
    public <V> IPage<V> pageMaps(IPage page, AbstractWrapper<T, ?, ?> queryWrapper, Class<V> voClass) {
        IPage<Map<String, Object>> pages = Db.pageMaps(page, queryWrapper);
        List<Map<String, Object>> maps = pages.getRecords();
        List<V> voList = BeanUtil.copyToList(maps, voClass);
        Page pageNew = new Page();
        pageNew.setCurrent(pages.getCurrent());
        pageNew.setSize(pages.getSize());
        pageNew.setTotal(pages.getTotal());
        pageNew.setPages(pages.getPages());
        pageNew.setRecords(voList);
        return pageNew;
    }
}
